var util = require('../../utils/util.js');
var ajax = require('../../utils/ajax.js');
var common = require('../../utils/common.js');
var PageBase = require('../../common/PageBase.js');
var app = getApp();

Page(common.extendPage(PageBase, {
    data: {
        winHei: 0,
        dataLoaded: false,
        userInfo: {},
        currentTab: 0,
        orderList: [{
            price: '',
            amount: '',
            huohao: '',
            guige: '',
            sales_code: '',
            unique: 'unique_0',
            currentTab: 0
        }],
        index: 0,
        currentTab: 0,
        salesCode: [],
        totalPrice: 0,
        totalAmount: 0,
        checking: false,
        remark: '',
        focus: false,
        submiting: false
    },

    onLoad() {
        this._getSalesCode();
    },
    onShow() {},
    /**
     * 获取销售码
     */
    _getSalesCode: function() {
        var url = "/market/Order/getSalesCode";
        var data = {};
        var orderList = this.data.orderList;
        ajax.P({
            url,
            data,
            success: (data) => {
                var salesCode = data.result.sales_code;
                orderList[0].sales_code = salesCode[0];
                this.setData({
                    salesCode,
                    dataLoaded: true
                })
            }
        })
    },
    /**
     *添加更多
     */
    _add: function() {
        var orderList = this.data.orderList;
        var salesCode = this.data.salesCode;
        orderList.push({
            price: '',
            amount: '',
            huohao: '',
            guige: '',
            sales_code: '',
            currentTab: 0
        });
        orderList[orderList.length - 1].unique = orderList[orderList.length - 2].unique.split('_')[0] + '_' + parseInt(parseInt(orderList[orderList.length - 2].unique.split('_')[1]) + 1);
        orderList[orderList.length - 1].sales_code = salesCode[0];
        this.setData({
            orderList
        })
    },
    /**
     *删除
     */
    _del: function(evt) {
        var orderList = this.data.orderList;
        var index = evt.currentTarget.dataset.index;
        if (orderList.length == 1) {
            return false;
        }
        orderList.splice(index, 1);
        this.setData({
            orderList
        })
    },
    /**
     * input框失去焦点时候，获取value
     */
    _isPriceEntry: function(evt) {
        this.setCurrentData(evt, 'price');
    },
    _isAmountEntry: function(evt) {
        this.setCurrentData(evt, 'amount');
    },
    _isGoodNumEntry: function(evt) {
        this.setCurrentData(evt, 'huohao');
    },
    _isOrderSkuEntry: function(evt) {
        this.setCurrentData(evt, 'guige');
    },
    /**
     * 切换销售码
     */
    _tabSwitch: function(evt) {
        var {
            current,
            index
        } = evt.currentTarget.dataset;
        var orderList = this.data.orderList;
        var salesCode = this.data.salesCode;
        orderList[index].currentTab = current;
        orderList[index].sales_code = salesCode[current];
        this.setData({
            orderList
        })
    },
    /**
     * 失去焦点时候，获取备注value
     */
    _confirmRemark: function(evt) {
        this.setData({
            remark: evt.detail.value
        })

    },
    /**
     * 弹出模态框
     */
    _submitOrder: function() {

        var orderList = this.data.orderList;
        var totalPrice = this.data.totalPrice;
        var totalAmount = this.data.totalAmount;
        this.isWrong = false;
        orderList.forEach((item) => {
            if (item.price == '') {
                util.alert('您输入的价格不能为空');
                this.isWrong = true;
            } else if (item.price == 0) {
                util.alert('您输入的价格不能为0');
                this.isWrong = true;
            } else if (item.amount == '') {
                util.alert('您输入的数量不能为空');
                this.isWrong = true;
            } else if (item.amount == 0) {
                util.alert('您输入的数量不能为0');
                this.isWrong = true;
            }
            totalPrice += parseFloat(item.price) * parseInt(item.amount) || 0;
            totalAmount += parseInt(item.amount) || 0;
        })

        if (this.isWrong) {
            return false;
        }

        this.setData({
            totalAmount,
            checking: true,
            totalPrice:totalPrice.toFixed(2)
            
        })

    },
    /**
     * 取消模态框提醒
     */
    _cancel: function() {
        if (this.data.submiting) {
            util.alert('正在提交中，请稍后...');
            return false;
        }
        this.setData({
            checking: false,
            totalPrice: 0,
            totalAmount: 0
        })
    },
    /**
     * 确认提交订单信息
     */
    _confirm: function() {
        var orderList = this.data.orderList;
        var remark = this.data.remark;
        //深拷贝，把orderList赋值给goods，指向不同的引用
        var goods = JSON.parse(JSON.stringify(orderList));

        goods.forEach((item) => {
            delete item.unique;
            delete item.currentTab;
        })

        goods = JSON.stringify(goods);

        var url = '/market/Order/quickOrder';
        var data = {
            goods,
            remark
        }

        if (this.data.submiting) {
            util.alert('正在提交中，请稍后...');
            return false;
        }

        this.setData({
            submiting: true
        })
        
        ajax.P({
            url,
            data,
            success: (data) => {
                var order_id = data.result.order_id;
                this.setData({
                    checking: false,
                    submiting: false
                })
                wx.redirectTo({
                    url: '/pages/shopOrder/index?orderId=' + order_id
                })
            },
            fail: (data) => {
                this.setData({
                    submiting: false
                })
            }
        })
    },
    setCurrentData: function(evt, item) {
        var orderList = this.data.orderList;
        var index = evt.currentTarget.dataset.index;
        orderList[index][item] = evt.detail.value;
        this.setData({
            orderList
        })
    }
}));